package faf.web;

import java.io.IOException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import faf.business.SheetManager;
import faf.business.SurveyManager;
import faf.business.UserManager;
import faf.entities.Account;
import faf.entities.Question;
import faf.entities.Sheet;
import faf.entities.Survey;

public class CreateSurveyController extends SimpleFormController {

	SurveyManager surveyManager;
	UserManager userManager;
	SheetManager sheetManager;
	

	public ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object object, BindException errors)
			throws Exception {

		Account account = (Account) request.getSession().getAttribute("user");
		Survey survey = (Survey) object;
		
		if (account == null) {
			return new ModelAndView("login");
		}

		if (request.getParameter("modifier") != null) {
			surveyManager.modifySurvey(survey);
		} else {
			survey.setAccount(account);
			surveyManager.createSurvey(survey);
			request.getSession().setAttribute("surveySession", survey);
		}

		List<Sheet> Sheets = sheetManager.numberOfPolled(survey);
		int numberOfPolled = Sheets.size();
		List<Account> listAccount = new ArrayList<Account>();
		for (Sheet s : Sheets) {
			listAccount.add(s.getAccount());
		}

		List<Question> LQ = surveyManager.findListQuestion(survey.getId());
		Map<String, Object> surveyModel = new HashMap<String, Object>();
		surveyModel.put("survey", survey);
		surveyModel.put("questions", LQ);
		surveyModel.put("size", LQ.size());
		surveyModel.put("listAccount", listAccount);
		surveyModel.put("numberOfPolled", numberOfPolled);

		return new ModelAndView("addquestion", "model", surveyModel);
	}

	public Object formBackingObject(HttpServletRequest request)
			throws ServletException, NotBoundException, RemoteException,
			Exception {
		Survey survey = new Survey();
		if (request.getParameter("modifier") != null) {
			long id = Long.decode(request.getParameter("modifier"));
			survey = surveyManager.get(id);
		}
		return survey;

	}

	protected Map<String, Object> referenceData(HttpServletRequest request)
			throws Exception {
		
		Map<String, Object> propetiesModel = new HashMap<String, Object>();
		
		if (request.getParameter("modifier") != null) {
		propetiesModel.put("modification", true);
		}
		
		return propetiesModel;
	}

	
	public void setSheetManager(SheetManager sheetManager) {
		this.sheetManager = sheetManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public void setSurveyManager(SurveyManager surveyManager) {
		this.surveyManager = surveyManager;
	}

}
